package com.oplink.webems.modules.cfg.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oplink.webems.common.persistence.Page;
import com.oplink.webems.common.utils.UserUtils;
import com.oplink.webems.modules.sys.dto.TreeDTO;
import com.oplink.webems.modules.sys.entity.Config;
import com.oplink.webems.modules.sys.service.ConfigService;
import com.oplink.webems.modules.sys.utils.DTOParseUtil;

/**
 * Title: 资源配置控制器
 * Description:
 * Copyright: Copyright (c) 2014
 * Company: Oplink Communications
 * 
 * @author
 * @serialData 2014-9-17 上午11:33:18
 * @version 1.0
 */
@Controller
@RequestMapping(value = "cfg/res")
public class CfgResController {

	@Autowired
	private ConfigService cfgService;

	@RequestMapping(value = "")
	public String neCfg(Config config, HttpServletRequest request, HttpServletResponse response, Model model) {
		// List<Config> listConfig = cfgService.findAllListById(UserUtils.getCurrentUserId(), "network_topo");
		config.setCatalog("network_topo");
		Page<Config> pages = cfgService.find(new Page<Config>(request, response, 10), config);
		model.addAttribute("pages", pages);
		return "modules/cfg/resIndex";
	}

	/**
	 * 返回根据catalog分类的树形控件
	 * 
	 * @return
	 */
	@RequestMapping(value = { "listDTO/{catalog}" }, method = { RequestMethod.GET })
	@ResponseBody
	public List<TreeDTO> listDTO(@PathVariable("catalog") String catalog) {
		List<Config> listConfig = cfgService.findAllListById(UserUtils.getCurrentUserId(), catalog);
		List<TreeDTO> listOut = new ArrayList<TreeDTO>();
		for (Config config : listConfig) {
			listOut.add(DTOParseUtil.toDTO(config));
		}
		return listOut;
	}
}
